package com.lo.make.blo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private String url = "http://152.136.132.183/bbw_test/#/user/loginIndex";
    private ValueCallback<Uri[]> mFilePathCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webView = findViewById(R.id.webview);


        WebSettings webSettings = webView.getSettings();
        webSettings.setDomStorageEnabled(true);//主要是这句
        webSettings.setBlockNetworkImage(false);//解决图片不显示
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setDatabaseEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }


        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

                mFilePathCallback = filePathCallback;
                Intent takePictureIntent = new Intent(Intent.ACTION_GET_CONTENT);
                takePictureIntent.addCategory(Intent.CATEGORY_OPENABLE);
                takePictureIntent.setType("image/*");
                Intent chooserIntent = Intent.createChooser(takePictureIntent, "Image Picker");

                // 如果 Intent.createChooser 返回 null，则直接调用系统默认的相册应用
                if (chooserIntent == null) {
                    startActivityForResult(takePictureIntent, 101);
                } else {
                    startActivityForResult(chooserIntent, 101);
                }
                return true;

            }
        });
        webView.loadUrl(url);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == RESULT_OK && data != null) {
            Uri[] selectedImages = null;
            if (data.getData() != null) {
                selectedImages = new Uri[]{data.getData()};
            } else if (data.getClipData() != null) { // 处理多选图片的情况
                int numImages = data.getClipData().getItemCount();
                selectedImages = new Uri[numImages];
                for (int i = 0; i < numImages; i++) {
                    selectedImages[i] = data.getClipData().getItemAt(i).getUri();
                }
            }
            mFilePathCallback.onReceiveValue(selectedImages); // 回调给 WebView
        } else {
            mFilePathCallback.onReceiveValue(null); // 用户取消或失败时，传递 null
        }
    }
}